<?php
/**
 * Created by PhpStorm.
 * User: andrei
 * Date: Jun 27, 2012
 * Time: 2:13:33 PM
 * To change this template use File | Settings | File Templates.
 */
 
class Db_Products_Themes extends Common {
	protected static $tableName = TAB_THEMES;
	protected static $dbFields = array(
        'id',
		'te_name',
		'te_type',
		'te_color_id',
        'deleted'
	);
	/**
	 * @var Db_Products_Themes
	 */
	public $objectArray;
	public $id;
	public $te_name;
	public $te_type;
	public $te_color_id;
    public $deleted;

    function __construct($value=NULL, $fieldName=NULL) {
        parent::__construct($value, $fieldName);
    }

	/**
	 * @static
	 * @return mixed
	 */
	public static function getAllThemes(){
        $sql = 'SELECT *, `te`.`id` AS `te_id`, `d`.`id` AS `d_id`, `cl`.`c_color_name` FROM ?f AS `te`
                LEFT JOIN ?f AS `d` ON `d`.`d_theme_id` = `te`.`id` AND `d`.`deleted` = 0
                LEFT JOIN ?f AS `cl` ON `cl`.`id` = `te`.`te_color_id` AND `cl`.`deleted` = 0
                WHERE `te`.`deleted` = 0 GROUP BY `te`.`id` ;';
        return self::$DB->select($sql, self::$tableName, TAB_DESIGNS, TAB_COLORS);
    }

	/**
	 * @static
	 * @return array
	 */
	public static function getThemesForSelect() {
		$result  = self::$DB->q("SELECT id,te_name FROM ?f WHERE deleted = 0",self::$tableName);
		$dataReturn = array();
		while($data = $result->fetch_object()) {
			$dataReturn[$data->id] = $data->te_name;
		}
		return $dataReturn;
	}

	/**
	 * @static
	 * @param $productId
	 * @return mixed
	 */
	public static function getAllThemesByDefault($productId) {
        $sql = "SELECT te.* FROM ?f te LEFT JOIN ?f pt ON te.id = pt.pt_theme_id WHERE pt.pt_product_id = ?d GROUP BY te.id ORDER BY pt.pt_is_default DESC ;";
        return self::$DB->select($sql, self::$tableName, TAB_PRODUCT_THEME_PHOTOS, $productId);

    }

	/**
	 * @static
	 * @param $productId
	 * @return bool|mixed
	 */
	public static function getThemesByProductId($productId = 0) {
		$sqlAdd = '';
		if ($productId) {
			$sqlAdd = "AND  pt.pt_product_id = " . self::$DB->escapeValue($productId);
		}
		return self::$DB->select("SELECT te.*,c.c_color_rgb FROM ?f te
								LEFT JOIN ?f pt ON te.id = pt.pt_theme_id
								LEFT JOIN ?f c ON te.te_color_id = c.id
								WHERE te.deleted = 0 {$sqlAdd} GROUP BY te.id",
								self::$tableName, TAB_PRODUCT_THEME_PHOTOS, TAB_COLORS
		);
	}

	/**
	 * @static
	 * @param null $productId
	 * @param $sizeId
	 * @return mixed
	 */
	public static function getValidThemes($productId = null, $sizeId) {
		$sqlJoin = null;
		$fieldName = null;
		$orderBy = null;
		$countTemplates = Db_Products_Templates::countAllByField('t_size_id', $sizeId);

		if(!empty($productId)) {
			$sqlJoin = "LEFT JOIN " . TAB_PRODUCT_THEME_PHOTOS . " AS `pt` ON `pt`.`pt_theme_id` = `te`.`id` AND `pt`.`pt_product_id` = {$productId}";
			$fieldName = ',`pt`.`pt_theme_id`';
			$orderBy = ' ORDER BY `pt`.`pt_is_default` DESC';
		}
		$sql = "SELECT t.id AS template_id, te.id AS te_id, te.te_name, COUNT(te.id) AS te_count {$fieldName}
				FROM ?f AS `te`
				INNER JOIN ?f AS `d` ON `te`.`id` = `d`.`d_theme_id`
				INNER JOIN ?f AS `t` ON `t`.`id` = `d`.`d_template_id`
				{$sqlJoin}
				WHERE `te`.`deleted` = 0 AND `d`.`deleted` = 0 AND `t`.`deleted` = 0 AND `t`.`t_size_id` = ?
				GROUP BY te.id
				{$orderBy} ;";

		if(!empty($productId)) {
			$usedThemes = self::getUsedThemes($productId);
		}
		$resultThemes  = self::$DB->q($sql, self::$tableName, TAB_DESIGNS, TAB_TEMPLATES, $sizeId );
		$themes = array();
		$i = 0;
		while($data = $resultThemes->fetch_assoc()) {
			$themes[$data['te_count']][] = array('id' => $data['te_id'], 'te_name' => $data['te_name']);
			if(isset($data['pt_theme_id'])) {
				$themes[$data['te_count']][$i]['pt_theme_id'] = $data['pt_theme_id'];
				if(isset($usedThemes[$data['pt_theme_id']])) {
					$themes[$data['te_count']][$i]['used'] = $usedThemes[$data['pt_theme_id']];
				}
			}
			$i++;
		}
		return (!empty($themes[$countTemplates]) ? $themes[$countTemplates] : false);
	}

	/**
	 * @static
	 * @param $productId
	 * @return array
	 */
	public static function getUsedThemes($productId) {
		$sql = "SELECT  pt.pt_theme_id FROM ?f AS pt
				INNER JOIN  ?f AS pp ON pp.pp_product_id = pt.pt_product_id
				WHERE pt.pt_product_id = ? GROUP BY pp.pp_template_id, pt.pt_theme_id;";
		$themes  = self::$DB->q($sql, TAB_PRODUCT_THEME_PHOTOS, TAB_PRODUCT_PAGES, $productId);
		$resultThemes = array();
		while($data = $themes->fetch_assoc()) {
			$resultThemes[$data['pt_theme_id']] = true;
		}
		return $resultThemes;
	}

	/**
	 * @static
	 * @param $productId
	 * @return bool|mixed
	 */
	public static function getAllThemesByProductId($productId) {
		if (!$productId) {
			return false;
		}
		return self::$DB->select("SELECT te.*, pt.pt_theme_id FROM ?f te
								LEFT JOIN ?f pt ON te.id = pt.pt_theme_id AND pt.pt_product_id = ?d
								WHERE te.deleted = 0 GROUP BY te.id",
			self::$tableName, TAB_PRODUCT_THEME_PHOTOS, $productId
		);
	}

	/**
	 * @static
	 * @param $productId
	 * @return bool
	 */
	public static function deleteProductsThemes($productId) {
		self::$DB->q("DELETE FROM ?f WHERE pt_product_id = ? ;", TAB_PRODUCT_THEME_PHOTOS, $productId);
		return (self::$DB->mysqlModifiedRows() > 0 ? true : false );
	}
}